package org.unique.common.tools;

import java.util.List;
import java.util.Map;

import org.unique.common.tools.CollectionUtil;

import com.mongodb.BasicDBObject;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;

/**
 * mongo util
 * @author:rex
 * @date:2014年8月14日
 * @version:1.0
 */
public final class MongoUtil {

	@SuppressWarnings("unchecked")
	public static <K, V> List<Map<K, V>> cursor2List(DBCursor cursor) {
		if (null != cursor && cursor.count() > 0) {
			List<Map<K, V>> result = CollectionUtil.newArrayList();
			while (cursor.hasNext()) {
				result.add(cursor.next().toMap());
			}
			return result;
		}
		return null;
	}
	
	public static DBObject map2DBObject(Map<String, Object> map) {
		DBObject dbObject = new BasicDBObject();
		for (String key : map.keySet()) {
			if(null != map.get(key)){
				dbObject.put(key, map.get(key));
			}
		}
		return dbObject;
	}

}
